Option Strict On

Imports System.Text.RegularExpressions
Imports System.Web


''' <summary>
''' Contains DataFormatting 
''' </summary>
''' <remarks></remarks>
<CLSCompliant(True)> _
Public Class DataFormat

#Region "Data Conversions"


    Public Shared Function CheckBool(ByVal value As Object) As Boolean
        If IsDBNull(value) OrElse value.ToString = "" Then
            Return False
        Else
            Return CBool(value)
        End If
    End Function

    Public Shared Function CellIntValue(ByVal row As DataRow, ByVal value As String) As Integer
        If (IsDBNull(row(value))) Then
            Return 0
        Else
            Return CInt(row(value))
        End If
    End Function

    Public Shared Function ObjToString(ByVal value As Object) As String
        If value Is Nothing OrElse IsDBNull(value) Then

            Return ""
        Else
            Return value.ToString()
        End If
    End Function

    Public Shared Function ObjToDecimal(ByVal value As Object) As Decimal
        Try
            If (value Is Nothing OrElse IsDBNull(value) OrElse value.ToString = "") Then
                Return 0
            Else
                Return CDec(value)
            End If
        Catch
            Return 0D
        End Try
    End Function

    Public Shared Function ObjToBoolean(ByVal value As Object) As Boolean
        Try
            If (value Is Nothing OrElse IsDBNull(value) OrElse value.ToString = "") Then
                Return False
            Else
                Return CBool(value)
            End If
        Catch
            Return False
        End Try
    End Function

    Public Shared Function ObjToInteger(ByVal value As Object) As Integer
        Try
            If (value Is Nothing OrElse IsDBNull(value) OrElse value.ToString = "") Then
                Return 0
            Else
                Return CInt(value)
            End If
        Catch
            Return 0I
        End Try
    End Function

    Public Shared Function ObjToDouble(ByVal value As Object, Optional ByVal Round As Boolean = False) As Double
        Try
            If value Is Nothing OrElse IsDBNull(value) OrElse value.ToString = "" Then
                Return 0D
            Else
                If Round = False Then
                    Return CDbl(FormatNumber(CDbl(value), 2, TriState.UseDefault, TriState.True, TriState.True))
                Else
                    Return CDec(FormatNumber(CDbl(value), 2, TriState.UseDefault, TriState.True, TriState.True))
                End If
            End If
        Catch
            Return 0D
        End Try
    End Function

    Public Shared Function RowsToTable(ByVal rows() As DataRow, _
                                        ByVal dtTemplate As DataTable, _
                                        ByVal newTableName As String) As DataTable
        Dim dt As DataTable = dtTemplate.Clone
        dt.TableName = newTableName
        If dt.Columns.Count > 0 Then dt.PrimaryKey = New DataColumn() {dt.Columns(0)}
        If rows Is Nothing OrElse rows.Length = 0 Then Return dt
        For i As Integer = 0 To rows.Length - 1
            dt.ImportRow(rows(i))
        Next
        Return dt
    End Function

#End Region

#Region "String Formatting"

    Public Shared Function BuildAddressString(ByVal CompanyName As String, _
                                               ByVal Address1 As String, _
                                               ByVal Address2 As String, _
                                               ByVal City As String, _
                                               ByVal State As String, _
                                               ByVal Zip As String, _
                                               Optional ByVal Phone As String = "", _
                                               Optional ByVal Fax As String = "") As String
        Dim address As String = ""
        If CompanyName <> "" Then address &= CompanyName & vbCrLf
        If Address1 <> "" Then address &= Address1 & vbCrLf
        If Address2 <> "" Then address &= Address2 & vbCrLf
        If City <> "" Then address &= City & ", " & State & "  " & Zip & vbCrLf
        If Phone <> "" Then address &= "Phone: " & GetPhoneFormat(Phone)
        If Fax <> "" Then address &= "  Fax: " & GetPhoneFormat(Fax) & vbCrLf
        Return address
    End Function

    Public Shared Function GetPhoneFormat(ByVal _Phone As String) As String
        If _Phone = "" Then Return ""
        Dim _p As String = ""
        If _Phone.Length >= 10 Then
            _p = Mid(_Phone, 1, 3) & "-" & Mid(_Phone, 4, 3) & "-" & Mid(_Phone, 7, _Phone.Length - 6)
        ElseIf _Phone.Length < 10 And _Phone.Length > 3 Then
            _p = Mid(_Phone, 1, 3) & "-" & Mid(_Phone, 4, _Phone.Length - 3)
        Else
            _p = _Phone
        End If
        Return _p
    End Function

#End Region

#Region "String Parsing"

    Public Shared Function GetAbsoluteURL(ByVal URL As String) As String
        If URL.StartsWith("/") Then
            URL = "http://www.mises.org" + URL
        End If
        Return URL
    End Function

    Public Shared Function GetFileFromURL(ByVal URL As String) As String
        Dim LastIndex As Integer = URL.LastIndexOf("/")
        URL = URL.Substring(LastIndex + 1, (URL.Length - LastIndex - 1))
        Return URL
    End Function

    Public Shared Function CountLinks(ByVal content As String) As Integer
        Dim _
            pattern As String = _
                "(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])"
        Return Regex.Matches(content, pattern, RegexOptions.IgnoreCase).Count
    End Function

    Public Shared Function StripHTML(ByVal content As String) As String
        Return HttpUtility.HtmlEncode((Regex.Replace(content, "<(.|\n)*?>", String.Empty)))
    End Function

    Public Shared Function EncodeForJavaScript(ByVal [text] As String) As String
        Return [text].Replace("\", "\\").Replace(ChrW(10), "\n").Replace(ChrW(13), "\r").Replace("'", "\'")
    End Function


#End Region
End Class
